package com.lanou.beautifulwardrobe.Brand.Adapter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.BrandShopActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.EditRecommendBrand;
import com.lanou.beautifulwardrobe.entify.RiseItem;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class RiseGridViewAdapter extends BaseAdapter {


    private List<RiseItem> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ViewGroup parent;

    public void addData(List<RiseItem> riseItems) {
        datas = riseItems;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_rise_item,null);
            this.parent=parent;
            holder = new Holder();
           holder.brandLogoImage= (ImageView) view.findViewById(R.id.brandLogoImage);
            view.setTag(holder);
        }else{
            holder= (Holder) view.getTag();
        }

        if (datas != null && datas.size() > 0) {
            getBitmap(datas.get(position).getLogo_url(),holder.brandLogoImage);
        }
        holder.brandLogoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(parent.getContext(), BrandShopActivity.class);
                long id= datas.get(position).getId();
                String imageUrl=datas.get(position).getLogo_url();
                String title=datas.get(position).getTitle();
                intent.putExtra("id",id+"");
                intent.putExtra("title",title);
                intent.putExtra("imageUrl",imageUrl);
                parent.getContext().startActivity(intent);
            }
        });
        return view;
    }

    private class Holder {
        private ImageView brandLogoImage;

    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}
